<?php

Class Mbkb extends CI_Model
{
	function __construnct()
	{
		$this->load->database();
        parent::__construct();
        date_default_timezone_set('Asia/Jakarta');
	}

	function getbarang($term)
	{
		$sql = "select * from produk where nama like '$term%'";
		return $this->db->query($sql);
	}

	function tipetrn()
	{
		$sql = "select * from tipetrn";
		return $this->db->query($sql)->result();
	}
    
    function nobkb()
    {
        $sql = "select max(nobkb) as maxno from bkb";
        $row = $this->db->query($sql)->row();
        $no  = $row->maxno;
        
        $no    = substr($no,8,4);
        $no    += 1;
        $ym    = date("ym");
        $nobkb = sprintf("$ym%04d",$no);
        
        return $nobkb;
    }

    function bkb($soid,$nobkb){
        $username = $this->session->userdata('username');
        $sql      = "insert into bkb(nobkb,tanggal,soid,trx_date,username)values('$nobkb','$tanggal','$soid',NOW(),'$username')";
        return $this->db->query($sql);
    }
    
    function bkb_detail($nobkb,$soid,$prokode,$qty){
        $sql      = "insert into bkb_detail(nobkb,prokode,qty)values('$nobkb','$prokode','$qty')";
        return $this->db->query($sql);
    }
    
    function getproduk($prokode)
    {
        //$sql = "select * from produk where prokode = '$prokode'";
        $sql = "SELECT a.prokode,a.nama,a.pabrik,b.nama AS nama_pabrik 
                FROM produk a
                LEFT JOIN pabrik b ON a.pabrik=b.pabrik_id
                WHERE a.prokode = '$prokode' ";
        return $this->db->query($sql)->row();
    }
    
    function get_batch($prokode)
    {
        $sql = "select * from detailproduk where prokode = '$prokode'";
        //echo $sql;
        return $this->db->query($sql)->result();
    }
    
    function simpan($nobkb,$soid,$prokode,$qty,$keterangan,$nobatch,$nobatch)
    {
        $sql = "select nobkb from bkb where nobkb = '$nobkb'";
        $num = $this->db->query($sql)->num_rows();
        
        $username = $this->session->userdata('username');
        $tanggal  = date("Y-m-d");
        $trx_date = date("Y-m-d H:i:s");
        
        $this->db->trans_begin();        
        //$stat     = 1;
        //cek di table bkb
        $stat = 1;
        if($num == '0'){
            $sql   = "insert into bkb(nobkb,tanggal,soid,trx_date,username) values('$nobkb','$tanggal','$soid','$trx_date','$username');";
            //echo $sql1;exit;
            $stat .= $this->db->query($sql);            
            if(!$stat){
                $desc = $this->db->_error_message();
                $this->db->trans_rollback();
                
                return $desc;
            }
        }
        
        $sql2 = "insert into bkb_detail(soid,nobkb,prokode,qty,nobatch,keterangan) values('$soid','$nobkb','$prokode','$qty','$nobatch','$keterangan');";
        $stat .= $this->db->query($sql2);            
        if(!$stat){
            $desc =$this->db->_error_message();
            $this->db->trans_rollback();
            return $desc;
        }else{
            //update di table sale detail
    	    $query = "select a.price,a.diskon,b.hnappn from sale_detail a,produk b where a.prokode=b.prokode and soid = '$soid' and a.prokode = '$prokode'";
            $row   = $this->db->query($query)->row();
            $hpp   = $row->hnappn;
            $harga = $row->price;
            $diskon = $row->diskon;
            //harga beli * qty
            $hppxqty   = $hpp * $qty;        
            //harga jual * qty
            $pricexqty = $harga * $qty;
            //ambil diskon rp dari harga jual setelah dikalikan dengan qty
            $diskonrp  = $pricexqty * ($diskon/100);
            //hitung total setelah dipotong diskon
            $total     = $pricexqty - $diskonrp;
            //ambil ppn dari total
    		$ppn       = $total * 0.1;
            //hitung total setelah di tambah ppn
            $totalppn  = $total * 1.1;
            
    		$now  = date("Y-m-d H:i:s");
    		$sql3  = "update sale_detail set 
                                        price2     = '$harga',
                                        qty2       = '$qty',
                                        diskon2    = '$diskon',
                                        diskonrp2  = '$diskonrp',
                                        total2     = '$total',
                                        hppxqty2   = '$hppxqty',
                                        pricexqty2 = '$pricexqty',
                                        trx_date2  = '$now',
                                        phg2       = phg,
                                        hppxqty2   = hpp * '$qty2'
                                        where soid = '$soid' and prokode = '$prokode'";
            //echo $sql3;exit;
                                        
            $stat .= $this->db->query($sql3);            
            if(!$stat){
                $desc =$this->db->_error_message();
                $this->db->trans_rollback();
                return $desc;
            }             
        }
        
        $this->db->trans_commit();
        $desc = "Transaksi Berhasil";
        return $desc;
    }
}